package services

import (
	"gitee.com/shijun123456/kcgin/logs"
	"gorm.io/gorm"
	"sinery.net.goft/src/models"
	"time"
)

type UserService struct {
	DB *gorm.DB `inject:"-"`
}

func NewUserService() *UserService {
	return &UserService{}
}

func (this *UserService) BatchInsert(insert []models.NoticeContextModel) int64 {
	go func() {
		context := &models.NoticeContextModel{}
		this.DB.First(context)
		logs.Info("conetxt: %+v", context)
		//获取第一条数据主键升序
		// select * from notice_context order by id limit 1
		//获取第一条记录
		this.DB.Take(&context) //没有指定排序 select * from table limit 1
		//获取最后一条记录 主键降序
		this.DB.Last(&context)
		//result.raowsaffecd error
		// errors.is result.error gorm.errrecordNotFound
		// 获取全部记录
		// result := db.find &users
		users := make([]models.NoticeContextModel, 6)
		//result:= this.DB.Find(&users)
		//logs.Info(result.RowsAffected)
		//logs.Info(result.Error)
		//errors.Is(result.Error,gorm.ErrRecordNotFound)
		//获取第一匹配的记录
		this.DB.Where("id = ?", "1").First(&context)
		// select * from notice_context where id = 1 order by id limit 1
		this.DB.Where("id <> ?", 1).Find(&users)
		//select * from users where id <> 不等于即可
		// in
		this.DB.Where("id IN ?", []int{1, 2, 3, 4}).Find(&users)
		// select * from users where id in();
		//like
		this.DB.Where("id LIKE ?", "%sin%").Find(&users)
		// and
		this.DB.Where("id = ? and notice_context= ?", "1", "sin").Find(&users)
		//time
		this.DB.Where("ctime > ? and ctime < ?", time.Now().AddDate(0, 0, -1).Unix(), time.Now().Unix())
		// betwen time
		this.DB.Where("created_at between ? and ?", time.Now().After(time.Now().AddDate(1, 0, 0)), time.Now().Unix())
		//map 信息
		this.DB.Where(&models.NoticeContextModel{ID: 1}).First(&context)
		this.DB.Where(map[string]interface{}{"id": "1"}).Find(&users)
		//主键切片条件
		this.DB.Where([]int64{1, 2, 3}).Find(&users)
		//注意当使用结构体作为条件查询 grom v2.0 只会查询非零值字段
		//
		this.DB.Where(map[string]interface{}{"name": "sin"})
		// 如果可以查询使用 map查询使用struct查询使用 []int64
		//切片 map struct
		//内联条件
		//用法和where类似
		this.DB.First(&context, 23) //根据主键获取记录 仅适用于整性主键
		//根据主键获取记录 如果非整型主键
		this.DB.First(&context, "id=?", "string_2222")
		// Plain SQL
		this.DB.Find(&users, "name=?", "sin")
		this.DB.Find(&users, "name <> ? and age > ?", "sin", 12)
		// struct
		this.DB.Find(&users, models.NoticeContextModel{NoticeContext: "sin"})
		//select * from users where notic_context = sin
		// map
		this.DB.Find(&users, map[string]interface{}{"age": 10})
		// not
		this.DB.Not("name = ?", "sin").First(&context)
		this.DB.Not(map[string]interface{}{"name": []string{"sin", "sin2"}}).Find(&users)
		// select * from users where name not in []string []int64 where not find condtion string map struct []int64 []string
		this.DB.Not(models.NoticeContextModel{ID: 1}).Find(&users)
		this.DB.Not([]int64{1, 2, 3}).Find(&users)
		// or 条件
		this.DB.Where("role = ?", "admin").Or("role=?", "sin").Find(&users)

		// struct
		this.DB.Where("name=?", "sin").Or(models.NoticeContextModel{NoticeContext: "s"}).Find(&users)

		//map
		this.DB.Where("name = sin").Or(map[string]interface{}{"sin": 22}).Find(&users)
		// select
		// table
		// gorup
		//order
		// or
		// find

	}()

	return this.DB.Create(&insert).RowsAffected
}
